import 'package:flutter/material.dart';
import '../utils/page_routes.dart';

class RouteAnimationDemo extends StatelessWidget {
  const RouteAnimationDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (Navigator.of(context).canPop()) {
          Navigator.of(context).pop();
          return false;
        }
        return true;
      },
      child: Scaffold(
        appBar: AppBar(
          title: const Text('路由动画示例'),
          leading: Navigator.of(context).canPop()
              ? IconButton(
                  icon: const Icon(Icons.arrow_back),
                  onPressed: () => Navigator.of(context).pop(),
                )
              : null,
        ),
        body: ListView(
          padding: const EdgeInsets.all(16),
          children: [
            _buildRouteButton(
              context,
              '滑动动画',
              () => _navigateToDetail(context, '滑动动画页面', RouteType.slide),
            ),
            _buildRouteButton(
              context,
              '淡入淡出',
              () => _navigateToDetail(context, '淡入淡出页面', RouteType.fade),
            ),
            _buildRouteButton(
              context,
              '缩放动画',
              () => _navigateToDetail(context, '缩放动画页面', RouteType.scale),
            ),
            _buildRouteButton(
              context,
              '旋转缩放',
              () => _navigateToDetail(context, '旋转缩放页面', RouteType.rotateScale),
            ),
            _buildRouteButton(
              context,
              '滑动渐变',
              () => _navigateToDetail(context, '滑动渐变页面', RouteType.slideUpFade),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildRouteButton(
    BuildContext context,
    String title,
    VoidCallback onPressed,
  ) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 12),
      child: ElevatedButton(
        onPressed: onPressed,
        style: ElevatedButton.styleFrom(
          padding: const EdgeInsets.symmetric(vertical: 16),
        ),
        child: Text(title),
      ),
    );
  }

  void _navigateToDetail(BuildContext context, String title, RouteType type) {
    PageRoute route;
    switch (type) {
      case RouteType.slide:
        route = SlideRightRoute(page: DetailPage(title: title));
        break;
      case RouteType.fade:
        route = FadeRoute(page: DetailPage(title: title));
        break;
      case RouteType.scale:
        route = ScaleRoute(page: DetailPage(title: title));
        break;
      case RouteType.rotateScale:
        route = RotateScaleRoute(page: DetailPage(title: title));
        break;
      case RouteType.slideUpFade:
        route = SlideUpFadeRoute(page: DetailPage(title: title));
        break;
    }
    Navigator.push(context, route);
  }
}

enum RouteType {
  slide,
  fade,
  scale,
  rotateScale,
  slideUpFade,
}

class DetailPage extends StatelessWidget {
  final String title;

  const DetailPage({
    super.key,
    required this.title,
  });

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (Navigator.of(context).canPop()) {
          Navigator.of(context).pop();
          return false;
        }
        return true;
      },
      child: Scaffold(
        appBar: AppBar(
          title: Text(title),
          leading: Navigator.of(context).canPop()
              ? IconButton(
                  icon: const Icon(Icons.arrow_back),
                  onPressed: () => Navigator.of(context).pop(),
                )
              : null,
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Text(
                title,
                style: Theme.of(context).textTheme.headlineMedium,
              ),
              const SizedBox(height: 20),
              ElevatedButton(
                onPressed: () => Navigator.pop(context),
                child: const Text('返回'),
              ),
              const SizedBox(height: 20),
              // 添加一个按钮来测试多级导航
              ElevatedButton(
                onPressed: () {
                  Navigator.push(
                    context,
                    SlideRightRoute(
                      page: DetailPage(title: '$title - 子页面'),
                    ),
                  );
                },
                child: const Text('打开子页面'),
              ),
            ],
          ),
        ),
      ),
    );
  }
} 